<template>
    <v-details :detailData="data.detailData" :inputConfig="inputConfig" :rules="rules" ref="input"
               @pop="setdata"></v-details>
</template>

<script>

import details from 'commons/Detail'
import baseMixin from "@/mixins/BaseMixin";

export default {
    name: "AsnDetails",
    mixins: [baseMixin],
    props: {
        data: {
            type: Object
        }
    },
    data() {
        return {
            flag: false,
            rules: {
                transferlinenumber: [
                    {required: true, trigger: 'blur'},
                ],
                fromsku: [
                    {required: true, validator: this.fromsku, trigger: 'blur'},
                ],
                fromloc: [
                    {required: true, validator: this.loc, trigger: 'blur'},
                ],
                fromlot: [
                    {required: true, trigger: 'blur'},
                ],
                fromqty: [
                    {required: true, validator: this.maxcount, trigger: 'blur'},
                ],
              

                tosku: [
                    {required: true, validator: this.tosku, trigger: 'blur'},
                ],
                toloc: [
                    {required: true, validator: this.loc, trigger: 'blur'},
                ],
                toqty: [
                    {required: true, trigger: 'blur'},
                ],
                lottable01: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable02: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable03: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable04: [
                    {validator: this.lottableDate, trigger: 'blur'},
                ],
                lottable05: [
                    {validator: this.lottableDate, trigger: 'blur'},
                ],
                lottable06: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable07: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable08: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable09: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
                lottable10: [
                    {validator: this.lottable, trigger: 'blur'},
                ],
            },
            addflag: true,
            editFlag: false
        }
    },
    computed: {
        inputConfig() {
            return [
                [
                    {
                        type: "input",
                        label: this.$t('outbound.userlinenumber'),
                        props: "transferlinenumber",
                        disabled: true
                    },
                    {
                        type: "table",
                        label: this.$t('ex.fromsku'),
                        props: "fromsku",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        readonly: true,
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "table",
                        label: this.$t('outbound.fromloc'),
                        props: "fromloc",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "table",
                        label: this.$t('ex.fromlot'),
                        props: "fromlot",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "table",
                        label: this.$t('out.cartonizationkey'),
                        props: "fromid",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('ex.fromqty'),
                        props: "fromqty",
                        disabled: this.flag
                    },
                ],
                [
                    {
                        type: "table",
                        label: this.$t('ex.tosku'),
                        props: "tosku",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        readonly: true,
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.tostorerkey ? this.$parent.$parent.$refs.detail.detailData.tostorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "table",
                        label: this.$t('config.toLoc'),
                        props: "toloc",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.tostorerkey ? this.$parent.$parent.$refs.detail.detailData.tostorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "table",
                        label: this.$t('ex.tolot'),
                        props: "tolot",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag || this.editFlag
                    },
                    {
                        type: "table",
                        label: this.$t('out.tocartonizationkey'),
                        props: "toid",
                        tableConfig: [
                            {
                                label: this.$t('config.sku'),
                                prop: "sku",
                                input: "input",
                            },
                            {
                                label: this.$t('config.shipper'),
                                prop: "storerkey",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lot'),
                                prop: "lot",
                                input: "input",
                            },
                            {
                                label: this.$t('config.loc'),
                                prop: "loc",
                                input: "input",
                            },
                            {
                                label: this.$t('in.lpn'),
                                prop: "id",
                                input: "input",
                            },
                            {
                                label: this.$t('config.num'),
                                prop: "qty",
                                input: "input",
                            },
                            {
                                label: this.$t('ex.qtyallocated'),
                                prop: "qtyallocated",
                                input: "input",
                            },
                            {
                                label: "qtypicked",
                                prop: "qtypicked",
                                input: "input",
                            },
                            {
                                label: "qtyexpected",
                                prop: "qtyexpected",
                                input: "input",
                            },
                        ],
                        url: `/querylotxlocidOfStorerkey/?/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey ? this.$parent.$parent.$refs.detail.detailData.fromstorerkey : undefined}`,
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('ex.toqty'),
                        props: "toqty",
                        disabled: this.flag
                    },
                ],
                [
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'01'}),
                        props: "lottable01",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'02'}),
                        props: "lottable02",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'03'}),
                        props: "lottable03",
                        disabled: this.flag
                    },
                    {
                        type: "date",
                        label: this.$t('config.batchProp',{no:'04'}),
                        props: "lottable04",
                        disabled: this.flag
                    },
                    {
                        type: "date",
                        label: this.$t('config.batchProp',{no:'05'}),
                        props: "lottable05",
                        disabled: this.flag
                    }
                ],
                [
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'06'}),
                        props: "lottable06",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'07'}),
                        props: "lottable07",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'08'}),
                        props: "lottable08",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'09'}),
                        props: "lottable09",
                        disabled: this.flag
                    },
                    {
                        type: "input",
                        label: this.$t('config.batchProp',{no:'10'}),
                        props: "lottable10",
                        disabled: this.flag
                    },
                ]
            ]
        }
    },
    methods: {
        id(rule, value, callback) {
            if (value != '') {
                // callback(new Error());
                this.$api.get(`/checkId/${value}`).then(res => {
                    
                        if (parseInt(res.bean) > 0) {
                            callback()
                        } else {
                            this.$message.error(this.$t('msg.msg22') + value);
                            this.data.detailData[rule.field] = ""
                            callback(new Error())
                        }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            } else {
                callback(new Error());
            }

        },
        loc(rule, value, callback) {
            if (value != '') {
                // callback(new Error());
                this.$api.get(`/queryLocCount/${value}`).then(res => {
                    
                        if (parseInt(res.bean) > 0) {
                            callback()
                        } else {
                            this.$message.error(this.$t('msg.msg23') + value);
                            this.data.detailData[rule.field] = ""
                            callback(new Error())
                        }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })
            } else {
                callback(new Error());
            }

        },
        lottable(rule, value, callback) {
            if (value == '') {
                if (this.data.detailData.lottable01 == "" &&
                    this.data.detailData.lottable02 == "" &&
                    this.data.detailData.lottable03 == "" &&
                    this.data.detailData.lottable04 == null &&
                    this.data.detailData.lottable05 == null &&
                    this.data.detailData.lottable06 == "" &&
                    this.data.detailData.lottable07 == "" &&
                    this.data.detailData.lottable08 == "" &&
                    this.data.detailData.lottable09 == "" &&
                    this.data.detailData.lottable10 == "") {
                    this.editFlag = false;
                }
                callback();
            } else {
                this.editFlag = true;
                this.data.detailData.tolot = "";
                callback();
            }
        },
        lottableDate(rule, value, callback) {
            if (value == null) {
                if (this.data.detailData.lottable01 == "" &&
                    this.data.detailData.lottable02 == "" &&
                    this.data.detailData.lottable03 == "" &&
                    this.data.detailData.lottable04 == null &&
                    this.data.detailData.lottable05 == null &&
                    this.data.detailData.lottable06 == "" &&
                    this.data.detailData.lottable07 == "" &&
                    this.data.detailData.lottable08 == "" &&
                    this.data.detailData.lottable09 == "" &&
                    this.data.detailData.lottable10 == "") {
                    this.editFlag = false;
                }
                callback();
            } else {
                this.editFlag = true;
                this.data.detailData.tolot = "";
                callback();
            }
        },
        maxcount(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                var re = /^[0-9]*$/;
                let te = value;
                let flag = re.test(te)
                if (flag) {
                    if (value < 0) {
                        this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
                        this.data.detailData[rule.field] = ""
                        callback(new Error());
                    } else {
                        callback();
                    }
                } else {
                    this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
                    this.data.detailData[rule.field] = ""
                    callback(new Error());
                }
            }
        },
        fromsku(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                /*this.$api.get(`/sku/isExist/${value}/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey}`).then(res => {
                    console.log(res);
                    
                        if (res.bean) {
                            this.$set(this.data.detailData, "fromsku", res.bean.sku);
                            this.$set(this.data.detailData, "fromloc", res.bean.loc);
                            this.$set(this.data.detailData, "fromlot", res.bean.lot);
                            this.$set(this.data.detailData, "fromid", res.bean.id);
                            callback();
                        } else {
                            this.$message.error(`不存在的货品:${value}`);
                            callback(new Error())
                        }
                    }
                }).catch(e => {
                    this.$message.error(e);
                    callback(new Error())
                })*/
                callback();
            }
        },
        tosku(rule, value, callback) {
            if (value === '') {
                callback(new Error());
            } else {
                // this.$api.get(`/sku/isExist/${value}/${this.$store.state.wms}/${this.$parent.$parent.$refs.detail.detailData.fromstorerkey}`).then(res => {
                //     
                //         if (res.bean) {
                //             this.$set(this.data.detailData, "tosku", data.sku);
                //             this.$set(this.data.detailData, "toloc", data.loc);
                //             this.$set(this.data.detailData, "tolot", data.lot);
                //             this.$set(this.data.detailData, "toid", data.id);
                //             callback();
                //         } else {
                //             this.$message.error(`不存在的货品:${value}`);
                //             callback(new Error())
                //         }
                //     }
                // }).catch(e => {
                //     this.$message.error(e);
                //     callback(new Error())
                // })
                callback();
            }
        },
        setdata(data, id, close, listId) {
            if (listId == 0) {
                if (id == 1) {
                    this.$set(this.data.detailData, "fromsku", data.sku);
                    this.$set(this.data.detailData, "fromloc", data.loc);
                    this.$set(this.data.detailData, "fromlot", data.lot);
                    this.$set(this.data.detailData, "fromid", data.id);
                    this.$set(this.data.detailData, "fromqty", data.qty);
                } else if (id == 2) {
                    this.$set(this.data.detailData, "fromloc", data.loc);
                } else if (id == 3) {
                    this.$set(this.data.detailData, "fromlot", data.lot);
                } else if (id == 4) {
                    this.$set(this.data.detailData, "fromid", data.id);
                }
            } else if (listId == 1) {
                if (id == 0) {
                    this.$set(this.data.detailData, "tosku", data.sku);
                    this.$set(this.data.detailData, "toloc", data.loc);
                    this.$set(this.data.detailData, "tolot", data.lot);
                    if (!this.editFlag) {
                        this.$set(this.data.detailData, "toid", data.id);
                    }
                } else if (id == 1) {
                    this.$set(this.data.detailData, "toloc", data.loc);
                } else if (id == 2) {
                    this.$set(this.data.detailData, "tolot", data.lot);
                } else if (id == 3) {
                    this.$set(this.data.detailData, "toid", data.id);
                }
            }
            close(false);
        },
    },
    mounted() {
        if (this.$parent.$parent.$refs.detail.detailData.status == "0") {
            this.flag = false;
        } else {
            this.flag = true;
        }
    },
    watch: {},
    components: {
        'v-details': details
    }
}
</script>

<style scoped>
</style>
